package com.auerdi.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.LinearLayout;

import com.auerdi.AnimationUtils.AnimationUtils;
import com.auerdi.R;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.AddreddList;
import com.example.egghttp.retrofit.beans.banner.HomeBanner;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by highsixty on 2018/3/15.
 * mail  gaolulin@sunmi.com
 */

public class AddressDialog extends AppCompatDialogFragment {


    //    private static String mString;
    Unbinder unbinder;
    @BindView(R.id.container)
    LinearLayout container;
    @BindView(R.id.recy)
    RecyclerView recy;

    List<HomeBanner> mRegionList = new ArrayList<>();
    private String mAddressId;


    public AddressDialog() {
        super();
    }

    public static AddressDialog newInstance(String addressId, AddreddListener payWayListener) {
        AddressDialog fragment = new AddressDialog();
        Bundle args = new Bundle();
        L.d("hcia", "addressId:" + addressId);
        args.putString("addressId", addressId);
        fragment.setArguments(args);
        fragment.setListener(payWayListener);
        return fragment;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
//        windowParams.dimAmount = 0.0f;
        window.setAttributes(windowParams);
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAddressId = getArguments().getString("addressId");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_address_layout, container, false);
        unbinder = ButterKnife.bind(this, view);
        AnimationUtils.slideToUp(view);
        RetrofitClient.getPDAClient().create(AdService.class).addressRegionList(mAddressId)
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BOb<AddreddList>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BOb<AddreddList> addreddListBOb) {
                        List<HomeBanner> regionList = addreddListBOb.getBody().getRegion_list();


                        mRegionList.clear();
                        mRegionList.addAll(regionList);
                        recy.getAdapter().notifyDataSetChanged();

                    }
                });
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        recy.setLayoutManager(new GridLayoutManager(getActivity(), 1) {


        });
        recy.setHasFixedSize(true);


        recy.setAdapter(new CommonAdapter<HomeBanner>(getActivity(), R.layout.item_region, mRegionList) {
            @Override
            protected void convert(ViewHolder viewHolder, final HomeBanner payWay, int position) {


                viewHolder.setText(R.id.title, payWay.getName() + "");


                viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                        listener.onAddRess(Integer.parseInt(payWay.getId()));
                        listener.onAddRess(payWay.getName());
                        dismiss();


                    }
                });


            }
        });


        container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AnimationUtils.slideToDown(view, new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        dismiss();

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });


            }
        });


    }


    @Override
    public void onDestroyView() {


        super.onDestroyView();
        unbinder.unbind();
    }


    private AddreddListener listener = null;

    public void setListener(AddreddListener listener) {
        this.listener = listener;
    }

    public interface AddreddListener {
        void onAddRess(Integer id);

        void onAddRess(String address);


    }
}
